package com.zhss.eshop.order.price.promotion.discount;


import com.alibaba.fastjson.JSONObject;
import com.zhss.eshop.order.domain.dto.OrderItemDTO;
import com.zhss.eshop.order.price.promotion.PromotionActivityCalculator;
import com.zhss.eshop.order.price.promotion.PromotionActivityResult;
import com.zhss.eshop.promotion.domain.dto.PromotionActivityDTO;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 单品促销活动的价格计算组件
 * @author suyuan
 *
 */
@Component
public class DirectDiscountPromotionActivityCalcualtor implements PromotionActivityCalculator {

//	@Autowired
//	private JsonExtractor jsonExtractor;

	/**
	 * 计算促销活动的减免金额
	 */
	@Override
	public PromotionActivityResult calculate(OrderItemDTO item,
											 PromotionActivityDTO promotionActivity) throws Exception {
		BigDecimal totalAmount = BigDecimal.valueOf(item.getPurchaseQuantity() * item.getPurchasePrice());

		JSONObject rule = JSONObject.parseObject(promotionActivity.getRule());
		BigDecimal discountRate = BigDecimal.valueOf(rule.getDouble( "discountRate"));

		BigDecimal discountAmountRate = BigDecimal.valueOf(1.0).subtract(discountRate);

		Double discountAmount = totalAmount.multiply(discountAmountRate).doubleValue();

		return new PromotionActivityResult(discountAmount);
	}

}
